package org.lab.util;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.text.DateFormat;

public class StringUtils {

        public static String toString(Object obj) {
                if (obj == null) {
                        return "";
                }
                if (obj.getClass().getName().equals("java.lang.String")) {
                        return toString((String) obj);
                }
                if (obj.getClass().getName().equals("java.lang.Integer")) {
                        return toString((Integer) obj);
                }
                if (obj.getClass().getName().equals("java.lang.Long")) {
                        return toString((Long) obj);
                }
                if (obj.getClass().getName().equals("java.sql.Date")) {
                        return toString((Date) obj);
                }
                if (obj.getClass().getName().equals("java.util.Date")) {
                        return toString((java.util.Date) obj);
                }
                if (obj.getClass().getName().equals("java.lang.Float")) {
                        return toString((Float) obj);
                }
                if (obj.getClass().getName().equals("java.sql.Timestamp")) {
                        return toString((Timestamp) obj);
                }
                if (obj.getClass().getName().equals("java.lang.Double")) {
                        return toString((Double) obj);
                }

                return obj.toString();
        }

        public static String toString(int obj) {
                return String.valueOf(obj);
        }

        public static String toString(long obj) {
                return String.valueOf(obj);
        }

        public static String toString(double obj) {
                return String.valueOf(obj);
        }

        public static String toString(float obj) {
                return String.valueOf(obj);
        }

        public static String toString(boolean obj) {
                return String.valueOf(obj);
        }

        public static String toString(char obj) {
                return String.valueOf(obj);
        }

        private static String toString(String obj) {
                if (obj == null) {
                        return "";
                }
                return obj;
        }

        private static String toString(Integer obj) {
                if (obj == null) {
                        return "";
                }
                return obj.toString();
        }

        private static String toString(Long obj) {
                if (obj == null) {
                        return "";
                }
                return obj.toString();
        }

        private static String toString(Date obj) {
                if (obj == null) {
                        return "";
                }
                DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd");
                return df.format(obj);
        }

        private static String toString(Double obj) {
                if (obj == null) {
                        return "";
                }
                return obj.toString();
        }

        private static String toString(Float obj) {
                if (obj == null) {
                        return "";
                }
                return obj.toString();
        }

        private static String toString(Timestamp obj) {
                if (obj == null) {
                        return "";
                }
                return obj.toString();
        }

        private static String toString(java.util.Date obj) {
                if (obj == null) {
                        return "";
                }
                return getDateString(obj);
        }

        private static String getDateString(java.util.Date adate) {
                SimpleDateFormat df = null;
                String returnValue = "";

                if (adate != null) {
                        df = new SimpleDateFormat("yyyy-MM-dd");
                        returnValue = df.format(adate);
                }

                return (returnValue);
        }

        /**
         * 在 source 后边填充 target 总长len (target 单字符)
         * 
         * @param source
         * @param target
         * @param len
         * @return
         */
        public static String fillAfter(String source, String target, int len) {
                StringBuffer bf = new StringBuffer(len);
                for (int i = 0; i < len; i++) {
                        bf.append(target);
                }
                String temp = bf.toString();
                temp = temp.substring(source.length(), len);
                return source + temp;
        }

        /**
         * 在 source 前边填充 target 总长len (target 单字符)
         * 
         * @param source
         * @param target
         * @param len
         * @return
         */
        public static String fillBefore(String source, String target, int len) {
                StringBuffer bf = new StringBuffer(len);
                for (int i = 0; i < len; i++) {
                        bf.append(target);
                }
                String temp = bf.toString();
                temp = temp.substring(0, len - source.length());
                return temp + source;
        }
        
        public static String tofirstUpperCase(String source,int index){
                String temp=source.substring(0,index);
                source=temp.toUpperCase()+source.substring(index,source.length());
                return source;
        }
        public static String tofirstLowerCase(String source,int index){
                String temp=source.substring(0,index);
                source=temp.toLowerCase()+source.substring(index,source.length());
                return source;
        }

}
